﻿import flash.display.BitmapData;
import mx.utils.Delegate;

dynamic class My3D.BitmapMaterial extends MovieClip{
	public static var M_UPDATA:String="updataedMaterial";//--------------------------------------------------------------------------------------
	//																					private
	
	private var $link:String;
	private var $picurl:String;
	private var $rootmc:MovieClip;
	public var $bmp:BitmapData;
	private var $pic:pictureBox;
	private var $picloadListenerObj:Object;
	private var $showCase:Boolean = false;
	private var $showCaseHeight:Number = 30;
	private var $showCaseWidth:Number = 30;
	
	private var $loaded:Boolean = false;
	//--------------------------------------------------------------------------------------
	//																					public
	
	/**
	 * Material width
	 */
	public function get width():Number {
		if (this.Loaded) {
			return this.$bmp.width;
		}else {
			return -1;
		}
	}
	
	/**
	 * Material height
	 */
	public function get height():Number {
		if (this.Loaded) {
			return this.$bmp.height;
		}else {
			return -1;
		}
	}
	
	public function get showCaseHeight():Number { return $showCaseHeight; }
	
	public function set showCaseHeight(value:Number):Void {
		$showCaseHeight = value;
		this.$pic.Height = value;
	}
	public function get showCaseWidth():Number { return $showCaseWidth; }
	
	public function set showCaseWidth(value:Number):Void {
		$showCaseWidth = value;
		this.$pic.Width = value;
	}
	/**
	 * show case the bitmapMaterial
	 */
	public function get ShowCase():Boolean {
		return this.$showCase;
	}
	/**
	 * show case the bitmapMaterial
	 */
	public function set ShowCase(value:Boolean) {
		this.$showCase = value;
		this.$pic._visible = value;
	}
	/**
	 * bitmap link string
	 */
	public function get Link():String {
		return this.$link;
	}
	/**
	 * bitmap link string
	 */
	public function set Link(value:String) {
		this.$link = value;
	}
	public function get Url():String {
		return this.$picurl;
	}
	public function set Url(value:String) {
		this.$picurl = value;
	}
	/**
	 * the material for testure
	 */
	public function get Bmp():BitmapData {
		return this.$bmp;
	}
	/**
	 * if material is ready
	 */
	public function get Loaded():Boolean { return $loaded; }
	
	
	//--------------------------------------------------------------------------------------
	//																					function 
	
	public function BitmapMaterial(rootmc:MovieClip) {
		
		initsay();
		if (rootmc == undefined) {
			rootmc = this;
		}
		if (rootmc == undefined) {
			_say.esay("not set root mc.");
			return;
		}
		this.$rootmc = rootmc;
		initvars();
		_say.msay("inited!");
		AsBroadcaster.initialize(this);
	}
	private function initvars() {
		this.$pic = new pictureBox(this.$rootmc);
		this.$picloadListenerObj = new Object();
		this.$picloadListenerObj[pictureBox.M_PROGRESS] = Delegate.create(this,this.loadingporgress);
		this.$pic.addListener(this.$picloadListenerObj);
		this.$pic.Width = this.$showCaseWidth;
		this.$pic.Height = this.$showCaseHeight;
	}
	private function loadingporgress(ll:Number, tt:Number) {
		_say.msay("loading:[" + ll + "/" + tt + "]");
		if (ll == tt && ll > 0 && tt > 0) {
			this.loadedPics();
			_say.msay("Meterial ready.");
			this.broadcastMessage(M_UPDATA, this);
			this[M_UPDATA](this);
		}
	}
	/**
	 * loaded picture , create bitmap Material for testure;
	 */
	private function loadedPics() {
		this.$bmp = this.$pic.draw(false);
		this.$loaded = true;
		this.$pic._visible = this.$showCase;
	}
	/**
	 * load picture Material
	 */
	public function load() {
		
		if (this.$link != "" && this.$link != undefined) {
			this.dispose();
			loadlink();
			return;
		}else if (this.$picurl != "" && this.$picurl != undefined) {
			//_say.esay("No finished mothed![loadpicurl]");
			this.dispose();
			loadUrl();
			return;
		}else {
			_say.esay("No image link or url to load!");
		}
	}
	
	public function dispose() {
		this.$bmp.dispose();
		this.$pic.unload();
	}
	private function loadUrl() {
		this.$pic.PicUrl = this.$picurl;
		this.$pic.load();
	}
	private function loadlink() {
		this.$pic.PicLink = this.$link;
		this.$pic.load();
	}
	private function showbmpcase() {
		
	}
	////////////////////////////////////////////////////////////////      output
	private var _say:Say;
	private function initsay() {
		_say = new Say("BitmapMaterial");
		_say.addListener(this);
		this[Say.M_SAY] = this.sayfun;
	}
	private function sayfun(o) {
		trace(o);
	}
	
}